<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">特点</label>
                        <div class="layui-input-block">
                            <input type="text" name="trait" placeholder="" class="layui-input" lay-verify="required">
                            <!--<div name="trait" id="trait" value=""></div>-->
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="" class="layui-input" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">图标</label>
                        <div class="layui-input-block">
                            <img class="img-3" src=""/>
                            <input type="text" style="display:none" name="icon" value="" lay-verify="required" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="icon"  permission="app.admin.upload.image">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="attachment-choose-image_path"  permission="app.admin.upload.attachment">
                                <i class="layui-icon layui-icon-align-left"></i>选择图片
                            </button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">音色ID(讲话人)</label>
                        <div class="layui-input-block">
                            <input type="text" name="timbreid" placeholder="" class="layui-input" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">主播语速</label>
                        <div class="layui-input-block">
                            <input type="number" style="display:none" name="speak_speed" value="10" />
                            <div id="speak_speed" style="top:15px;"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">支持语言</label>
                        <div class="layui-input-block">
                            <div name="lang" id="lang" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">推荐场景</label>
                        <div class="layui-input-block">
                            <input type="text" name="scenes" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label">采样率</label>-->
                        <!--<div class="layui-input-block">-->
                            <!--<input type="text" name="tone_rate" placeholder="" class="layui-input">-->
                        <!--</div>-->
                    <!--</div>-->
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label">音色情感</label>-->
                        <!--<div class="layui-input-block">-->
                            <!--<input type="text" name="emotion" placeholder="" class="layui-input">-->
                        <!--</div>-->
                    <!--</div>-->

                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <!--<button type="reset" class="pear-btn pear-btn-md">-->
                        <!--重置-->
                    <!--</button>-->
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            // 相关接口
            const PRIMARY_KEY = "id";
            const PRIMARY_KEY_VALUE = layui.url().search[PRIMARY_KEY];
            const SELECT_API = "/app/admin/timbre/index/select" + location.search;
            const UPDATE_API = "/app/admin/timbre/index/update";
            const TRAIT_API = "/app/admin/timbre/index/trait";
            let PART_NUMS = 0;


            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    async:true,
                    headers: get_header({"id":PRIMARY_KEY_VALUE}),
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });

                        // 字段 图片地址 image_path
                        layui.use(["upload", "layer"], function() {
                            let input = layui.$("#icon").prev();
                            input.prev().attr("src", input.val());
                            layui.$("#attachment-choose-image_path").on("click", function() {
                                parent.layer.open({
                                    type: 2,
                                    title: "选择附件",
                                    content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                                    area: ["95%", "90%"],
                                    success: function (layero, index) {
                                        parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                            input.val(data.url).prev().attr("src", data.url);
                                        });
                                    }
                                });
                            });
                            layui.upload.render({
                                elem: "#icon",
                                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                                url: "/app/admin/upload/image",
                                field: "__file__",
                                headers: get_header({}),
                                done: function (res) {
                                    if (res.code > 0) return layui.layer.msg(res.msg);
                                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                                }
                            });
                        });

                        // 字段 创作内容 trait
                        // layui.use(["jquery", "xmSelect", "popup"], function() {
                        //     PART_NUMS = res.data[0]['part_nums'];
                        //     layui.$.ajax({
                        //         url: TRAIT_API,
                        //         dataType: "json",
                        //         async:true,
                        //         headers: get_header({}),
                        //         success: function (res) {
                        //             let value = layui.$("#trait").attr("value");
                        //             let initValue = value ? value.split(",") : [];
                        //             layui.xmSelect.render({
                        //                 el: "#trait",
                        //                 name: "trait",
                        //                 filterable: true,
                        //                 data: res.data,
                        //                 initValue: initValue,
                        //                 model: {"icon":"hidden","label":{"type":"text"}},
                        //                 clickClose: true,
                        //                 radio: true,
                        //             })
                        //             if (res.code) {
                        //                 layui.popup.failure(res.msg);
                        //             }
                        //         }
                        //     });
                        // });

                        layui.use(["jquery", "slider"], function() {
                            var slider = layui.slider;

                            let value = $('input[name="speak_speed"]').val();
                            //主播语速
                            slider.render({
                                elem: '#speak_speed',  //绑定元素
                                min: 8,
                                max: 15,
                                step: 1,
                                value: value != undefined ? value : 10,
                                tips: true,
                                theme: "#2d8cf0",
                                input: true,
                                change: function (value) {
                                    layui.$('input[name="speak_speed"]').val(value);
                                }
                            });
                        });

                        // 字段 状态 status
                        layui.use(["jquery", "xmSelect"], function() {
                            let value = layui.$("#lang").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#lang",
                                name: "lang",
                                filterable: true,
                                initValue: initValue,
                                data: [{"value":"普通话","name":"普通话"},{"value":"英文","name":"英文"},{"value":"泰语","name":"泰语"},{"value":"印尼语","name":"印尼语"}],
                                model: {"icon":"hidden","label":{"type":"text"}},
                                clickClose: true,
                                radio: true
                            })
                        });

                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    // if(data.field.trait==false){
                    //      layui.popup.failure('特点必选');
                    //     return false;
                    // }
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    let loading = layer.load();
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            layer.close(loading);
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
